#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2003 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   lsswmanprop command
#
#   Syntax:  lsswmanprop { { -t { man | top | ver } [ -F <format> ] | --help }
#
#            -t { man | top | ver }   - Specifies the type of properties
#                                            the user wishes displayed
#            --help                   - Prints this message
#            -F <format>              - a delimiter separated list of names
#                                            for the properties to query.
#                                            Valid values for -t man are:
#                                                 ip
#                                                 hostname
#                                                 uptime
#                                                 version
#                                            Valid values for -t top are:
#                                                 network
#                                                 plane
#                                                 endpoints_up
#                                                 sw_links_up
#                                            If -F is not specified, all 
#                                            properties are shown.  -F is
#                                            not valid for -t ver
#                    
#
####################################################################
#
#   Initial version - 09/06/02
#
####################################################################

use Getopt::Long;

sub bynumber { $a <=> $b; }

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

################################################################################
#
# subroutines to parse management properties desired (-F parm for -t man)
#
################################################################################
sub parse_man_props {
	$parm_length = length($props_parm);
	if (substr($props_parm,0,1) eq ":") {
                        # There was a colon at the beginning
			#      of the -F value
#			print "Colon in the beginning\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	if (substr($props_parm,$parm_length-1) ne ":") {
                        # There was not a colon at the end
			#      of the -F value
#			print "No colon at the end\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	# Find number of colons used
	$NUM_COLONS = 0;
	while ($props_parm =~ m/:/g) {
		$NUM_COLONS += 1;
	}

	@prop_list = split /:/, $props_parm, $MAX_MAN_PROPS + 1;

	# Now examine fields passed
        for ($i = 0; $i < $NUM_COLONS; $i++) {
		$prop = $prop_list[$i];
		# Were the fields specified properly?
		if ($prop eq "") {
#			print "There was a NULL field passed.\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
		}
		else {
			# Is this a valid field name?
			$FOUND = 0;
			for ($j = 0; $j < $MAX_MAN_PROPS; $j++) {
				if ($prop eq $DEF_MAN_PROPS[$j]) {
					if ($FOUND_MAN_PROPS[$j] == 1) {
						# Second time for this prop
#						print "SECOND time\n"; #DEBUG
						print $USAGE;
						$ret = 1;
						&exit_routine;
					}
					else {
						$FOUND = 1;
						$FOUND_MAN_PROPS[$j] = 1;
						$MAN_ORDER[$NUM_MAN_PROPS] = $j;
					}
				}
			}

			# Was the field name a valid field name?
			if ($FOUND == 0) {
#				print "Field name NOT found\n";     #DEBUG
				print $USAGE;
				$ret = 1;
				&exit_routine;
			}

			$NUM_MAN_PROPS += 1;
			
#			# Were an incorrect number of fields passed?
#		        if ($NUM_PROPS > $MAX_PROPS) {
#				print "Too MANY props passed\n";        #DEBUG
#				print $USAGE;
#			}
		}
	}
}

################################################################################
#
# subroutines to parse topology properties desired (-F parm for -t top)
#
################################################################################
sub parse_top_props {
	$parm_length = length($props_parm);
	if (substr($props_parm,0,1) eq ":") {
                        # There was a colon at the beginning
			#      of the -F value
#			print "Colon in the beginning\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	if (substr($props_parm,$parm_length-1) ne ":") {
                        # There was not a colon at the end
			#      of the -F value
#			print "No colon at the end\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
	}

	# Find number of colons used
	$NUM_COLONS = 0;
	while ($props_parm =~ m/:/g) {
		$NUM_COLONS += 1;
	}

	@prop_list = split /:/, $props_parm, $MAX_TOP_PROPS + 1;

	# Now examine fields passed
        for ($i = 0; $i < $NUM_COLONS; $i++) {
		$prop = $prop_list[$i];
		# Were the fields specified properly?
		if ($prop eq "") {
#			print "There was a NULL field passed.\n";   #DEBUG
			print $USAGE;
			$ret = 1;
			&exit_routine;
		}
		else {
			# Is this a valid field name?
			$FOUND = 0;
			for ($j = 0; $j < $MAX_TOP_PROPS; $j++) {
				if ($prop eq $DEF_TOP_PROPS[$j]) {
					if ($FOUND_TOP_PROPS[$j] == 1) {
						# Second time for this prop
#						print "SECOND time\n"; #DEBUG
						print $USAGE;
						$ret = 1;
						&exit_routine;
					}
					else {
						$FOUND = 1;
						$FOUND_TOP_PROPS[$j] = 1;
						$TOP_ORDER[$NUM_TOP_PROPS] = $j;
					}
				}
			}

			# Was the field name a valid field name?
			if ($FOUND == 0) {
#				print "Field name NOT found\n";     #DEBUG
				print $USAGE;
				$ret = 1;
				&exit_routine;
			}

			$NUM_TOP_PROPS += 1;
			
#			# Were an incorrect number of fields passed?
#		        if ($NUM_PROPS > $MAX_PROPS) {
#				print "Too MANY props passed\n";        #DEBUG
#				print $USAGE;
#			}
		}
	}
}


################################################################################
#
# credentials subroutines
#
################################################################################
sub remove_creds {
        system "/bin/rm $RCMDTKFILE > /dev/null 2>&1";
        if ($DCE_tickets == 1) {
                `/bin/kdestroy`;
                 delete $ENV{'KRB5CCNAME'};
        }
} # end remove_creds subroutine

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "lsswmanprop";
$BIN    = "/opt/hsc/bin"; 
$F_USED = 0;

$NUM_TOP_PROPS = 0;
$NUM_MAN_PROPS = 0;
$MAX_TOP_PROPS = 5;
$MAX_MAN_PROPS = 4;
$VER_COLS = 5;
@DEF_TOP_PROPS = ("network" , "plane" , "topology", "endpoints_up" , "sw_links_up" );
@DEF_MAN_PROPS = ("ip" , "hostname" , "uptime" , "version" );
@FOUND_TOP_PROPS = (0, 0, 0, 0, 0);
@FOUND_MAN_PROPS = (0, 0, 0, 0);
@TOP_HEAD = (" network ", " plane ", " topology ", " endpoints_up ", " sw_links_up ");
@MAN_HEAD = (" ip", " hostname", " uptime", " version");
@TOP_FORMAT = (" %8s ", " %7s ", " %14s", " %14s ", " %13s ");
@MAN_FORMAT = ("%15s ", " %24s ", " %31s", " %20s ");
@VER_HEAD = (" release", " version", " build-level", " driver", " efix-level");
@VER_FORMAT = (" %8s ", " %8s ", " %14s", " %10s ", " %11s ");

$USAGE = "Usage:  lsswmanprop { { -t { man | top | ver } [ -F <format> ] | --help }\n";

# First check for existence of parameters
if ($#ARGV == -1) {
#	print "No parms\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

# Get parameters used

Getopt::Long::Configure("no_ignore_case");
$result = GetOptions(
        "help" => \$help,
        "t=s" => \$tab_parm,
        "F=s" => \$Format,
);

#print "GetOptions rc is ", $result, " .\n";

if ($result == 0) {
	# Bad result from GetOptions
#	print "Bad result from GetOptions\n";     #DEBUG
	print $USAGE;      #USAGE
	$ret = 1;
	&exit_routine;
}

if ($help) {
	# help was asked for
#	print "help was specified\n";     #DEBUG
	print $USAGE;      #USAGE
	$ret = 0;
	&exit_routine;
}

$props_parm = $Format;  # colon-separated list of column names

if (defined $tab_parm) {
	$type_parm = $tab_parm;
	if (($type_parm ne "man") && ($type_parm ne "top") && ($type_parm ne "ver")) {
		# Invalid -t value
#		print "Bad -t value\n";     #DEBUG
		print $USAGE;
		$ret = 1;
		&exit_routine;
	}
}
else {
	# No -t or nothing else was specified after -t
#	print "No -t value\n";     #DEBUG
	print $USAGE;
	$ret = 1;
	&exit_routine;
}

# Now see if properties were passed
if (defined $Format) {
	# If they were and -t ver was specified, we've got an error
	if ($type_parm eq "ver") {
		# -F specified with -t ver
#		print "Can't specify -F with -t ver\n";   #DEBUG
		print $USAGE;
                $ret = 1;
                &exit_routine;
        }
	if ($type_parm eq "man") {
		&parse_man_props;
	}
	else {
		&parse_top_props;
	}
}
else {
	# -F was not specified, so use default
	if ($type_parm eq "man") {
		$NUM_MAN_PROPS = $MAX_MAN_PROPS;
       		@FOUND_MAN_PROPS = (1, 1, 1, 1);
       		@MAN_ORDER = (0, 1, 2, 3);
	}
	else {
		$NUM_TOP_PROPS = $MAX_TOP_PROPS;
       		@FOUND_TOP_PROPS = (1, 1, 1, 1, 1);
       		@TOP_ORDER = (0, 1, 2, 3, 4);
	}
}

# Now that we know what the issuer wants to see, let's get the data desired
if ($type_parm eq "top") {
	chop(@DATA = `CLASSPATH=/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo topoview`);
#	chop(@DATA = `CLASSPATH=/usr/websm/codebase/wsm.jar:/opt/hsc:/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo topoview`);

#	if ($DATA[0] == 0) {
#		print "SnmInfo returned 0.\n";             #DEBUG
#		print "There is no data to display.\n";
#		$ret = 1;
#		&exit_routine;
#	}

	$lines = 0;
	foreach (@DATA) {
		($f1, $f2, $f3, $f4, $f5) = split /\s+/;
		$OUTPUT[$lines]{0} = $f1;   # network
		$OUTPUT[$lines]{1} = $f2;   # plane
		$OUTPUT[$lines]{2} = $f3;   # topology
		$OUTPUT[$lines]{3} = $f4;   # endpoints_up
		$OUTPUT[$lines]{4} = $f5;   # sw_links_up
		$lines += 1;
	}

	if (defined $Format) {
		# -F parameters were passed, so we need to print out data
		#      based on the parameters
		for ($k = 1; $k < $lines; $k++) {
			$LINE_OUT = "";      # Blank out output line
			foreach (@TOP_ORDER) {
				$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_}.":";
			}
			print $LINE_OUT."\n";
		}
	}
	else {
		# No -F parameters were specified, so we'll generate the
		#     output line with each field's format and data for
		#     that field.  Print out a header first.
		$HEADER = "";
		foreach (@TOP_ORDER) {
			$HEAD_FIELD_OUT = sprintf "$TOP_FORMAT[$_]", $TOP_HEAD[$_];
			$HEADER = $HEADER.$HEAD_FIELD_OUT;
		}
		print $HEADER."\n";

		for ($k = 1; $k < $lines; $k++) {
			$LINE_OUT = "";      # Blank out output line
			foreach (@TOP_ORDER) {
				$FIELD_OUT = sprintf "$TOP_FORMAT[$_]", $OUTPUT[$k]{$_};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			print $LINE_OUT."\n";
		}
	}
}
elsif ($type_parm eq "man") {
	chop(@DATA = `CLASSPATH=/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo manageview`);
#	chop(@DATA = `CLASSPATH=/usr/websm/codebase/wsm.jar:/opt/hsc:/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo manageview`);

#	if ($DATA[0] == 0) {
#		print "SnmInfo returned 0.\n";             #DEBUG
#		print "There is no data to display.\n";
#		$ret = 1;
#		&exit_routine;
#	}

	$lines = 0;
	foreach (@DATA) {
		($f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8) = split /\s+/;
		$OUTPUT[$lines]{0} = $f1;   # ip
		$OUTPUT[$lines]{1} = $f2;   # hostname
		$OUTPUT[$lines]{2} = $f3." ".$f4." ".$f5." ".$f6." ".$f7;   # uptime
		$OUTPUT[$lines]{3} = $f8;   # version
		$lines += 1;
	}

	if (defined $Format) {
		# -F parameters were passed, so we need to print out data
		#      based on the parameters
		for ($k = 1; $k < $lines; $k++) {
			$LINE_OUT = "";      # Blank out output line
			foreach (@MAN_ORDER) {
				$LINE_OUT = $LINE_OUT.$OUTPUT[$k]{$_}.":";
			}
			print $LINE_OUT."\n";
		}
	}
	else {
		# No -F parameters were specified, so we'll generate the
		#     output line with each field's format and data for
		#     that field.  Print out a header first.
		$HEADER = "";
		foreach (@MAN_ORDER) {
			$HEAD_FIELD_OUT = sprintf "$MAN_FORMAT[$_]", $MAN_HEAD[$_];
			$HEADER = $HEADER.$HEAD_FIELD_OUT;
		}
		print $HEADER."\n";

		for ($k = 1; $k < $lines; $k++) {
			$LINE_OUT = "";      # Blank out output line
			foreach (@MAN_ORDER) {
				$FIELD_OUT = sprintf "$MAN_FORMAT[$_]", $OUTPUT[$k]{$_};
				$LINE_OUT = $LINE_OUT.$FIELD_OUT;
			}
			print $LINE_OUT."\n";
		}
	}
}
else {
	chop(@DATA = `CLASSPATH=/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo verview`);
#	chop(@DATA = `CLASSPATH=/usr/websm/codebase/wsm.jar:/opt/hsc:/usr/websm/codebase/pluginjars/SNMGUI.jar:/usr/websm/codebase/pluginjars/hsc.jar java com.ibm.hsc.auiml.databeans.snm.SnmInfo verview`);

#	if ($DATA[0] == 0) {
#		print "SnmInfo returned 0.\n";             #DEBUG
#		print "There is no data to display.\n";
#		$ret = 1;
#		&exit_routine;
#	}

	$lines = 0;
	foreach (@DATA) {
		($f1, $f2, $f3, $f4, $f5) = split /\s+/;
		$OUTPUT[$lines]{0} = $f1;   # Release
		$OUTPUT[$lines]{1} = $f2;   # Version
		$OUTPUT[$lines]{2} = $f3;   # Build-level
		$OUTPUT[$lines]{3} = $f4;   # Driver
		$OUTPUT[$lines]{4} = $f5;   # Efix-level
		$lines += 1;
	}

	#     Print out the lines we get from SnmInfo, minus the first one.
	#     Print out a header line first
	$HEADER = "";
	for ($m = 0; $m < $VER_COLS; $m++) {
		$HEAD_FIELD_OUT = sprintf "$VER_FORMAT[$m]", $VER_HEAD[$m];
		$HEADER = $HEADER.$HEAD_FIELD_OUT;
	}
	print $HEADER."\n";

	for ($k = 1; $k < $lines; $k++) {
		$LINE_OUT = "";      # Blank out output line
		for ($n = 0; $n < $VER_COLS; $n++) {
			$FIELD_OUT = sprintf "$VER_FORMAT[$n]", $OUTPUT[$k]{$n};
			$LINE_OUT = $LINE_OUT.$FIELD_OUT;
		}
		print $LINE_OUT."\n";
	}
}

$ret = 0;
&exit_routine;

